package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.query.GuaranteebillQueryObject;
import cn.wolfcode.crm.query.HistoryQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ICarService;
import cn.wolfcode.crm.service.IGuaranteebillService;
import cn.wolfcode.crm.service.ILatentClientService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("guaranteebill")
public class GuaranteebillController {
    @Autowired
    private ILatentClientService clientService;
    @Autowired
    private ICarService carService;
    @Autowired
    private IGuaranteebillService guaranteebillService;

    //保存map
    private static final Map<String, Object> billMap = new HashMap<>();

    //显示保单填写
    @RequestMapping("")
    public String view(){
        return "guaranteebill/edit";
    }
    //显示暂存单
    @RequestMapping("storage")
    public String storage(){
        return "guaranteebill/storage";
    }
    //显示批改单
    @RequestMapping("check")
    public String check(){
        return "guaranteebill/check";
    }
    //显示待审核单
    @RequestMapping("waitcheck")
    public String waitcheck(){
        return "guaranteebill/waitcheck";
    }
    //保单历史记录, 每一次状态都记录下来
    @RequestMapping("billhistory")
    public String billhistory(){

        return "guaranteebill/billhistory";
    }

    //查询正式客户
    @RequestMapping("queryClient")
    @ResponseBody
    public List<LatentClient> queryClient(int status){

        return clientService.selectByStatus(status);
    }

    //查询车辆信息
    @RequestMapping("queryCar")
    @ResponseBody
    public List<Car> queryCar(){
        return carService.selectAll();
    }

    //保存暂存单
    @RequestMapping("saveBill")
    @ResponseBody
    public JsonResult saveBill(){
        JsonResult result = new JsonResult();
        try {
            guaranteebillService.saveBill(billMap);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }

    //保存用户信息
    @RequestMapping("saveClient")
    @ResponseBody
    public JsonResult saveClient(LatentClient client){
        JsonResult result = new JsonResult();
        try {
            System.out.println(client);
            billMap.put("client",client);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }

    //保存汽车信息
    @RequestMapping("saveCar")
    @ResponseBody
    public JsonResult saveCar(Carinfo carinfo, Long carsizeId){
        JsonResult result = new JsonResult();
        try {
            System.out.println(carinfo);
           billMap.put("carinfo",carinfo);
           billMap.put("carsizeId",carsizeId);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }


    //保存产品信息
    @RequestMapping("saveProduct")
    @ResponseBody
    public JsonResult saveProduct(Long pid, Integer limitYear){
        JsonResult result = new JsonResult();
        try {
            System.out.println(limitYear);
            System.out.println(pid);
            billMap.put("pid",pid);
            billMap.put("limitYear",limitYear);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }


    //根据状态查询保单 + 高级查询
    @RequestMapping("queryBill")
    @ResponseBody
    public List<Guaranteebill> queryBill(GuaranteebillQueryObject qo){
       return guaranteebillService.selectByState(qo);

    }
    //根据状态查询历史记录
    @RequestMapping("queryHistory")
    @ResponseBody
    public PageResult queryHistory(@ModelAttribute("qo") HistoryQueryObject qo){
        return guaranteebillService.queryHistory(qo);
    }

    //提审
    @RequestMapping("auditClientById")
    @ResponseBody
    public JsonResult auditClientById(Long id){
        JsonResult result = new JsonResult();
        try {
            guaranteebillService.audit(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }

    //批改
    @RequestMapping("checkClientById")
    @ResponseBody
    public JsonResult checkClientById(Long id, String message){
        JsonResult result = new JsonResult();
        try {
            guaranteebillService.checkClient(id,message);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }

    //核保
    @RequestMapping("auidSuccess")
    @ResponseBody
    public JsonResult auidSuccess(Long id){
        JsonResult result = new JsonResult();
        try {
            guaranteebillService.auidSuccess(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }
    //拒保
    @RequestMapping("rejuest")
    @ResponseBody
    public JsonResult rejuest(Long id){
        JsonResult result = new JsonResult();
        try {
            guaranteebillService.rejuest(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存保单失败");
        }
        return result;
    }

    //查看批改意见
    @RequestMapping("queryAdvice")
    @ResponseBody
    public String queryAdvice(Long id){
        return guaranteebillService.queryAdvice(id);
    }
    //根据id查看carinfo信息
    @RequestMapping("queryCarInfo")
    @ResponseBody
    public Carinfo queryCarInfo(Integer carId){
        return guaranteebillService.queryCarInfo(carId);
    }
}
